package basics;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JavaFile {
    public static void main(String[] args) {
        HashMap<String,String> testMap = new HashMap<>();
        testMap.put("姓名","jack");
        testMap.put("年龄","20");

        //键值对的个数
        System.out.println(testMap.size());
        //key组成的集合
        System.out.println(testMap.keySet());
        //所有的值
        System.out.println(testMap.values());
        //获取一个键对应的值
        System.out.println(testMap.get("姓名"));
        System.out.println(testMap.get("年龄"));

        //第一种遍历方法
        //通过key来遍历
//        Set<String> keys = testMap.keySet();
//        Iterator<String> iterator = keys.iterator();
//        while (iterator.hasNext()){
//            String key = iterator.next();
//            String value = testMap.get(key);
//            System.out.println(key + "-" + value);
//        }

        //第二种遍历方法
        //通过Entry
        //Entry是用来封装键和值的对象
        Set<Map.Entry<String, String>> entries = testMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator1 = entries.iterator();
        while(iterator1.hasNext()){
            Map.Entry<String, String> entry = iterator1.next();
            System.out.println(entry.getKey()+"_"+entry.getValue());
        }
    }
}
